﻿#region USING STATEMENTS
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion

#region CODE BODY
namespace Fusion.Management
{
    public enum MouseButton { Left, Middle, Right }
    public static class FInput
    {
        //////////////////////////////////////////////////
        // Data Fields
        //////////////////////////////////////////////////
        private static KeyboardState OldKeyState = Keyboard.GetState(), NewKeyState = Keyboard.GetState();
        private static MouseState OldMouseState = Mouse.GetState(), NewMouseState = Mouse.GetState();
        private static Keys[] lastPressed;

        //////////////////////////////////////////////////
        // Field Properties
        //////////////////////////////////////////////////
        public static Keys[] LastPressed
        {
            get { return lastPressed; }
        }

        //////////////////////////////////////////////////
        // Public Static Methods
        //////////////////////////////////////////////////
        public static void BeginPoll()
        {
            NewKeyState = Keyboard.GetState();
            NewMouseState = Mouse.GetState();
            lastPressed = Keyboard.GetState().GetPressedKeys();
        }
        public static void EndPoll()
        {
            OldKeyState = NewKeyState;
            OldMouseState = NewMouseState;
        }

        // Keyboard Methods
        public static bool KeyPressed(Keys Check)
        {
            if (OldKeyState.IsKeyDown(Check) && NewKeyState.IsKeyUp(Check))
                return true;

            return false;
        }
        public static bool KeyDown(Keys Check)
        {
            if (NewKeyState.IsKeyDown(Check))
                return true;

            return false;
        }
        public static bool KeyUp(Keys Check)
        {
            if (NewKeyState.IsKeyUp(Check))
                return true;

            return false;
        }

        // Mouse Methods
        public static Point MousePos()
        {
            return new Point(NewMouseState.X, NewMouseState.Y);
        }
        public static Vector2 MousePosF()
        {
            return new Vector2(NewMouseState.X, NewMouseState.Y);
        }
        public static Boolean ButtonPressed(MouseButton Check)
        {
            switch (Check)
            {
                case MouseButton.Left:
                    return (OldMouseState.LeftButton == ButtonState.Pressed && NewMouseState.LeftButton == ButtonState.Released);

                case MouseButton.Middle:
                    return (OldMouseState.MiddleButton == ButtonState.Pressed && NewMouseState.MiddleButton == ButtonState.Released);

                case MouseButton.Right:
                    return (OldMouseState.RightButton == ButtonState.Pressed && NewMouseState.RightButton == ButtonState.Released);

                default:
                    return false;
            }
        }
        public static Boolean ButtonDown(MouseButton Check)
        {
            switch (Check)
            {
                case MouseButton.Left:
                    return (NewMouseState.LeftButton == ButtonState.Pressed);

                case MouseButton.Middle:
                    return (NewMouseState.MiddleButton == ButtonState.Pressed);

                case MouseButton.Right:
                    return (NewMouseState.RightButton == ButtonState.Pressed);

                default:
                    return false;
            }
        }
        public static Boolean ButtonReleased(MouseButton Check)
        {
            switch (Check)
            {
                case MouseButton.Left:
                    return (NewMouseState.LeftButton == ButtonState.Released);

                case MouseButton.Middle:
                    return (NewMouseState.MiddleButton == ButtonState.Released);

                case MouseButton.Right:
                    return (NewMouseState.RightButton == ButtonState.Released);

                default:
                    return false;
            }
        }
    }
}
#endregion

//////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 Jeffery Thomas Sventora
//////////////////////////////////////////////////////////////////////////////////////////////